package models.msgs;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;


public class Powercards {
	String [] power={"0","A","2","3", "4","5", "6", "7", "8",  "9", "10","J", "D", "K"};
	
	String [] power2={"0","K","D","J", "10","9", "8", "7", "6",  "5", "4","3", "2", "1"};
	ArrayList<String> powerl;
	ArrayList<String> powerl2;
	Deck deck;
	public Powercards(Deck deck)
	{  this.deck=deck;
	    powerl=new ArrayList<String>();
		for(int i=0; i<14; i++)
        { 
		  powerl.add(power[i]);}
		
		powerl2=new ArrayList<String>();
		for(int i=0; i<14; i++)
        { 
		  powerl2.add(power2[i]);}
	 }
	//public int Winneris( int pnumber)
	public int[] Winneris( int pnumber, String [] condition)
	{ int[] player;
	  int[] win;
	int max;
	int vars;
	   player= new int[pnumber+1];
	   player[0]=0;
	
		for(int i=1; i<=pnumber; i++ )
		{   if(!condition[i].equals("Fold"))
			player[i]=powerhand(deck.showcard(i, 0),deck.showcard(i, 1),deck.showcard(i, 2),deck.showcard(i, 3));
		   else player[i]=0;
		}
		max=player[0];
		for(int i=1; i<=pnumber; i++ )
		{
			if(player[i]>max) max=player[i];
		}
		vars=0;
		for(int i=1; i<=pnumber; i++ )
		{
			if(player[i]==max) ++vars;
		}
		  win= new int[vars];
		  int n=0;
		for(int i=1; i<=pnumber; i++ )
		{
			if(player[i]==max)  win[n++]=i;
		}
		
		//System.out.println(" " + vars);
		return win;
		
	}
	
	public int[] Winneris2( int pnumber)
	{int [] winner= new int[2];
	    winner[0]=1;
	    winner[1]=2;
		 return winner;
		
	}
	
	
	
	
	public Integer powerhand(String card1,String card2, String card3, String card4)
	{   int howlikecard;
	    String likecard;
	    int numerlikecard;
	    int numercard;
	 
	    int powern=0;
		Boolean [] cardtrue={true, true, true, true};
		
		String [][] cards= new String[4][2];
		cards[0]=card1.split(":");
		cards[1]=card2.split(":");
		cards[2]=card3.split(":");
		cards[3]=card4.split(":");
		
		howlikecard=0;	
		int howlikecard2=0;
		int howlikecard3=0;
	
		for(int i=0; i<3; i++)
		{
		  if(cardtrue[i]!=false) 
			
			{
			likecard=null;
			numerlikecard=-1;
			
			for(int j=0; j<4; j++)
				{
				
				if(j!=i) 
					  if(cardtrue[j]!=false)
						if( cards[i][1].equals(cards[j][1]))
							{ 
								likecard=cards[j][0];
									numerlikecard=j;
						
										break;
							}
				}
			if(likecard!=null)
		 		{
		 			
		 				for(int g=0; g<4; g++)
		 					{     howlikecard2=0;
		 					  if( numerlikecard!=g)	
		 						  if(cardtrue[g]!=false)
		 					      if( cards[numerlikecard][0].equals(cards[g][0]))
		 								{ 
		 										
		 					    	  for(int k=0; k<4; k++)
		 					    	  	{    
		 					    	  		if( g!=k)	
		 					    	  		  if(cardtrue[k]!=false)
		 					    	  				if( cards[g][1].equals(cards[k][1]))
		 					    	  					{ 
		 		 										 if(powerl.indexOf(cards[k][0])<powerl.indexOf( cards[g][0]))
		 		 											howlikecard2--;
		 		 											
		 					    	  					}  	
		 					    	  		}
		 					    	     howlikecard2++;
		 												
		 								}
		 					}
		 				
		 				for(int g=0; g<4; g++)
	 					{    howlikecard3=0;
	 					    // Boolean isgutcard=true; 
	 					  if( g!=i)
	 						  if(cardtrue[g]!=false)
	 					      if( cards[i][0].equals(cards[g][0]))
	 								{ 
	 					    	 for(int k=0; k<4; k++)
	 					    	  	{    
	 					    	  		if( g!=k)	
	 					    	  		  if(cardtrue[k]!=false)
	 					    	  				if( cards[g][1].equals(cards[k][1]))
	 					    	  					{ 
	 		 										 if(powerl.indexOf(cards[k][0])<powerl.indexOf( cards[g][0]))
	 		 											howlikecard3--;
	 		 										   
	 					    	  					} 
	 					    	  		} howlikecard3++;
	 											
	 								}
	 					}
		 				
		 				
		 			if(howlikecard2!=howlikecard3)
		 			{ if(howlikecard2>howlikecard3)
		 					{   cardtrue[numerlikecard]=false;
		 				
		 						}
		 			  else if(howlikecard2<howlikecard3)
		 			  	{
		 				 cardtrue[i]=false;
		 			  	}
		 				
		 			}
		 				
		 			
		 			else if(powerl.indexOf(likecard)<powerl.indexOf(cards[i][0]))
			 			{cardtrue[i]=false;
		 			     }
		 		else if(powerl.indexOf(likecard)>=powerl.indexOf(cards[i][0]))
		 						{
		 						cardtrue[numerlikecard]=false;
		 						}
		 	
		 		} 
			else {}
			
				
			
		 }
		}
		
		
		
		
		
		
		
		for(int i=0; i<3; i++)
		{	
		likecard=null;
		if(cardtrue[i]!=false) 
			for(int j=i+1; j<4; j++)
			{if(j!=i) 
				  if(cardtrue[j]!=false)
			  if( cards[i][0].equals(cards[j][0]))
			  	{
				  cardtrue[i]=false;
				 
				  break;
				  //sprawdza�, czy kolor mo�e by� lepszy, raczej tak
			  	}
			}
		 
		  
		}
		howlikecard=0;
		
		ArrayList <String> lc;
		lc= new ArrayList <String>();
		
		for(int i=0; i<4; i++)
		{
			  if(cardtrue[i]==true) 
			  {  if(cards[i][0].equals("A"))  lc.add("1");
			      else  lc.add(cards[i][0]);
			  
			  }
		}
		Collections.sort(lc);
		Comparator<? super String> arg1 = null;
		Collections.sort(lc, arg1);
		for(int i=0; i<4; i++)
		{
			  if(cardtrue[i]==false) 
			  { ++howlikecard;
			   lc.add("0");
			  
			  }
		}
		
		
		numercard=4-howlikecard;
		
		
		powern=numercard*100000000;
		    if(!lc.get(0).equals(0))
			powern=powern+powerl2.indexOf(lc.get(0))*1000000;
				
		    if(!lc.get(1).equals(0))
			powern=powern+powerl2.indexOf(lc.get(1))*10000;
			
		    if(!lc.get(2).equals(0))
			powern=powern+powerl2.indexOf(lc.get(2))*100;
			
		    if(!lc.get(3).equals(0))
			powern=powern+powerl2.indexOf(lc.get(3))*1;
			
			
		
		//return card1+  "|" + card2+  "|" + card3+  "|" +card4 + "  " + powern + " " + lc;
		return powern;
	}
	
	
	
	
	

	//public static void main(String[] args)
	//	{ Deck deck=new Deck(5);
		//Powercards pc= new Powercards(deck);
		// System.out.println(pc.powerhand("4:kier","3:trefl","2:trefl","2:karo"));
		// System.out.println(pc.powerhand("3:kier","3:karo","2:kier","2:karo"));
		// System.out.println(pc.powerhand("3:kier","4:karo","2:kier","2:karo"));
		// System.out.println(pc.powerhand("3:kier","A:karo","2:kier","2:karo"));
		// System.out.println(pc.powerhand("2:kier","D:karo","A:kier","2:karo"));
		//  System.out.println(pc.powerhand("9:kier","7:trefl","8:kier","8:trefl"));
		//  System.out.println(pc.powerhand("2:pik","4:pik","5:pik","4:karo"));
		//  System.out.println(pc.powerhand("2:pik","4:pik","4:trefl","4:karo"));  
			
		//} 	
	
}
